<?php
/**
 * Main Ressource SpreadSheet
 * This page display on the left a menu and on the right the content
 * of the page
 *
 * @author Xavier VILAIN
 * @package contacts
 */

use Fox\Html\TextArea;
use Hive\Ressource;
use Fox\Behaviors;
use Fox\Html\Button;
use Hive\Ressource\Source;
use Fox\Html\Wrapper;
use Fox\Core\Errors;
use Fox\Generator;
use Fox\Html\Navigation\Menu\Item;
use Fox\Html\Navigation\Menu\Item\Sub;
use Fox\Html\Navigation\Menu;

$master = 'ressource';
$action = 'main';
$social = '';
$xview = Xview::instance();
$customizing = Hive\Ressource\Customizing::instance();

$id = $xview->val('ressourceid',$xview->val('rid'));
$page = $xview->val('page');
$area = $xview->val('area');
try {
	/* @var $contact Hive\Ressource */
	$contact = Fox\Dao::load(Hive\Ressource::className(),$id);
	$tasks = $contact->tasks();
	$tasksNb = count($tasks);
	// Counter
	$wishesNb = $contact->countWishes();
	$interviewsNb = $contact->countInterviews();
	$documentNb = $contact->countDocuments();
	$jobsNb = $contact->countExperiences();
	$missionsNb = $contact->missionsCount();
	$nbOfMessages = $contact->countMessages();
	$note = $contact->note();
	foreach($contact->sources() as $source) {
		$reference = $source->reference;
		if(!empty($reference)) {
			$type = Fox\Dao::load(Source\Type::className(),$source->type);
			$social .= '<li>'.$type->render($reference).'</li>';
		}
	}
}
catch (Fox\Object\Exception\NotExist $e)
{
	Xview::instance()->systemError($e->getMessage(),Generator::url('ressource','search'));
}
if(!$contact->exists())
{
	Xview::instance()->accessDenied();
}

// Main Wrapper
$wrapper = new Wrapper('leftCol');

// Contact Summary
$contactMenu = new Menu('leftMenu',Msg(Hive::BUNDLE,Hive::CONTACT_CANDIDATE));

// News Feed
$contactMenu->add(new Item('newsfeed',Msg(Hive::BUNDLE,Hive::MENU_NEW_FEEDS),
						Generator::url($master,'newsfeed',null,
							array('rid' => $id))
						));//@TODO fill with the last changes
// Personnal Information
$contactMenu->add(new Item('personnal',Msg(Hive::BUNDLE,Hive::MENU_INFO_PERSO),
						Generator::url($master,'personnal',null,
							array('rid' => $id))
						));
// Job Information
$contactMenu->add(new Item('jobs',Msg(Hive::BUNDLE,Hive::MENU_INFO_JOB),
						Generator::url($master,'jobs',null,
							array('rid' => $id)),
						$jobsNb));
// Wishes
$contactMenu->add(new Item('wishes',Msg(Hive::BUNDLE,Hive::TITLE_WISHES),
						Generator::url($master,'wishes',null,array('rid' => $id)),
						$wishesNb));
// Skills
/*
$contactMenu->add(new Item('skills',Msg(Hive::BUNDLE,Hive::TITLE_SKILLS),
						Generator::url($master,'skills',null,array('rid' => $id))
					));
*/
// Interviews
$contactMenu->add(new Item('interviews',Msg(Hive::BUNDLE,Hive::TITLE_INTERVIEWS),
						Generator::url($master,'interviews',null,array('rid' => $id)),
						$interviewsNb
					));
// Missions
$contactMenu->add(new Item('missions',Msg(Hive::BUNDLE,Hive::TITLE_MISSIONS),
						Generator::url($master,'missions',null,
							array('rid' => $id)),
					$missionsNb
					));
// Messages
$contactMenu->add(new Item('messages',Msg(Hive::BUNDLE,Hive::MESSAGES),
						Generator::url($master,'messages',null,
							array('rid' => $id)),
						$nbOfMessages
					));
// Tasks
$contactMenu->add(new Item('tasks',Msg(Hive::BUNDLE,Hive::TITLE_TASKS),
						Generator::url($master,'tasks',null,
							array('rid' => $id)),
					$tasksNb
					));
// Tasks Detail ?
if($tasksNb > 0)
{
	/*
	$contactMenu->item('tasks')
		->add(new Sub('tasks-add',Msg(Fox\Msg::BUNDLE,Fox\Msg::ADD),
					Generator::url('missions','tasks','add',
						array('rid' => $id))
					));
	*/
}
// Documents
$contactMenu->add(new Item('documents',Msg(Hive::BUNDLE,Hive::MENU_DOCUMENTS),
						Generator::url($master,'documents',null,
							array('rid' => $id)),
					$documentNb));

$wrapper->add($contactMenu);
// -- Customer Part Menu ---
$customerMenu = new Menu('candidatsMenu',Msg(Hive::BUNDLE,Hive::MENU_LEAD));
$customerMenu->add(new Item('addresses',Msg(Hive::BUNDLE,Hive::MENU_INFO_PRO),
					Generator::url($master,'addresses','professional',
						array('rid' => $id))));
$wrapper->add($customerMenu);

// -- Action Part Menu ---
$customerMenu = new Menu('actionsMenu',Msg(Hive::BUNDLE,Hive::LABEL_NOTES));
$customerMenu->add(new TextArea('notes',false,$note->note,100,10))
				->hideLabel(true);
$wrapper->add($customerMenu);

// Manage Content Rendering
if(isset($_GET['uri']) && !empty($_GET['uri']))
{
	$parts = explode('-', $_GET['uri']);
	if(count($parts) > 2) {
		$url = Generator::url($parts[0],$parts[1],$parts[2],array('rid' => $id));
	}
	else
	{
		$url = Generator::url($parts[0],$parts[1],null,array('rid' => $id));
		$page = $parts[1];
	}
	if(!Xview::instance()->isModule($url)) {
		$url = null;
	}
	 
}

if(empty($url))
{
	$params = array('rid' => $id,'return' => true);
	list($url,$page) = $customizing->defaultSummaryPage($params);
} elseif(empty($page)) {
	if(empty($area)) {
		$page = 'newsfeed';
	} else {
		$page = $page;
	}
}
// Select Clicked Item
$selectedPage = $wrapper->item($page);
if(empty($selectedPage)) {
	$page = 'newsfeed';
}
$wrapper->item($page)->selected();

if(!empty($social)) {
	$social = '<ul class="social">'.$social.'</ul><div class="clear"></div>';
}

//@TODO Check Authorization
$params = array('rid' => $id,'inlineid' => $id);
$carousel = $xview->val('carousel',false,false);
if(!empty($carousel)) {
	$params['carousel'] = true;
}
$deleteButton = new Button('delete','?');
$deleteButton->color(Button::COLOR_RED)
			->behaviors(Behaviors::action(
				Generator::url('ressource','delete',null,$params),
		true,null,null,true));

// Summary Block
$currentPositionInfo = $summaryLines = null;
$currentPosition = $contact->currentPosition();
if($currentPosition->exists()) {
	if($currentPosition->companyName() != null) {
		$currentPositionInfo = Msg(Hive::BUNDLE,Hive::MSG_POSITION,$currentPosition->jobtitle,$currentPosition->companyName());
	} else {
		$currentPositionInfo = $currentPosition->jobtitle;
	}
}
$genderAndAge = null;
if($contact->gender() != null) {
	$genderAndAge = $contact->gender();
}
if($contact->age() > 20) {
	$ageLabel = Msg(Hive::BUNDLE,Hive::X_YEARS_OLD,$contact->age());
	$genderAndAge .= empty($genderAndAge) ? $ageLabel : ' - '.$ageLabel;
}
if(!empty($genderAndAge)) {
	$summaryLines .='<p>'.$genderAndAge.'</p>';
}
if($contact->yearsOfExperience() > 0) {
	$summaryLines .= '<p>'.Msg(Hive::BUNDLE,Hive::MSG_YEARS_EXP,$contact->yearsOfExperience()).'</p>';
}
$symbol = Hive\Ressource\Customizing::instance()->salaryCurrencySymbol();
$salaryAndWhish = null;
if(!empty($currentPosition) && $currentPosition->fullSalary() > 0) {
	$currentSalary = \Fox\Utilities\Format::formatUsingCurrency($currentPosition->fullSalary(),$symbol);
	$summaryLines .= '<p>'.Msg(Hive::BUNDLE,Hive::CURRENTLY_SALARY_AT,$currentSalary).'</p>';
}
if($contact->wish()->fullSalary() > 0) {
	$wishSalary = \Fox\Utilities\Format::formatUsingCurrency($contact->wish()->fullSalary(),$symbol);
	$summaryLines .= '<p>'.Msg(Hive::BUNDLE,Hive::WISH_X,$wishSalary).'</p>';
}

$html = '<div id="leftColContainer" >
	<div id="leftCol">
		<div id="pagelet_welcome_box">
			<div class="hive-contact-box">
				<div class="clearfix uiImageBlock fbxWelcomeBox">
					<a href="?mod=ressource&act=main&page=detail&uri=company-detail&rid='.$id.'" class="uiImageBlockMediumImage lfloat slink">
						<img src="'.$contact->picture().'" style="width:40px" />
					</a>
					<span class="title">'.$contact->name().'</span>
				</div>
				<div class="i">'.$currentPositionInfo.$summaryLines.'</div>
				'.$social.'
				<span id="contact-data" class="hidden" data-id="'.$id.'" data-notid="'.$note->notid.'"></span>
			</div>
		</div>
		<div id="pagelet_navigation">
			<div class="uiFutureSideNav">
			'.$wrapper->render().'
			</div>
		</div>
		<div id="delete_action" class="center">'.$deleteButton->render().'</div>
	</div>
</div>';
echo $html;
echo '<div id="contentCol" class="hasLeftCol">';
echo $xview->module($url);
echo '</div>';